package com.yaolong.api.modules.gen.genengines;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.yaolong.api.modules.gen.genengines.CustomAbstractTemplateEngine;
import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.MultiTemplateLoader;
import freemarker.cache.StringTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import com.yaolong.api.modules.gen.config.CustomConfigs;
import com.yaolong.api.modules.gen.config.CustomOutputTemplateResultConfig;
import com.yaolong.api.modules.gen.config.CustomTemplateConfig;
import com.yaolong.api.modules.gen.constants.GenConstants;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

/**
 * @author yaolong
 * @version V1.0.0
 * @program EasyCode
 * @description 自定义Beetl引擎
 * @create 2022-12-31 11:37
 **/
public class CustomBeetlTemplateEngine extends CustomAbstractTemplateEngine {


    @Override
    public CustomBeetlTemplateEngine init(@NotNull ConfigBuilder configBuilder) {
        return this;
    }


    @Override
    public void writer(@NotNull Map<String, Object> objectMap, @NotNull String templatePath, @NotNull File outputFile) throws Exception {
//        CustomConfigs customConfigs = (CustomConfigs) objectMap.get(GenConstants.CUSTOM_CONFIG_FIELD);
//        Template template = this.configuration.getTemplate(templatePath);
//        FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
//        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, ConstVal.UTF8);
//        try {
//            String outputType = customConfigs.getCustomGlobalConfig().getOutputType();
//            //如果类型是字符串说明是预览，否则输出文件
//            if (outputType.equals(GenConstants.OUTPUT_TYPE_FILE)){
//                template.process(objectMap, outputStreamWriter);
//            }else {
//                setResultConfig(objectMap, outputFile, template);
//            }
//
//        } catch (Throwable var9) {
//            try {
//                fileOutputStream.close();
//            } catch (Throwable var8) {
//                var9.addSuppressed(var8);
//            }
//            throw var9;
//        }
//        fileOutputStream.close();
    }

    @Override
    public @NotNull
    String templateFilePath(@NotNull String filePath) {
        return filePath + ".btl";
    }

    /**
     * <p>
     * 设置生成结果配置 输出类型为string时生效
     * </p>
     *
     * @param objectMap
     * @param outputFile
     * @param template
     * @return void
     * @version 1.0.0 <br>
     * @date 2022/12/30 0:22 <br>
     * @author yaolonga <br>
     */
    private void setResultConfig(@NotNull Map<String, Object> objectMap, @NotNull File outputFile, Template template) throws TemplateException, IOException {
        //字符串Writer用于返回
        StringWriter stringWriter = new StringWriter();
        template.process(objectMap, stringWriter);
        String fileName = outputFile.getName();
        String templateStr = stringWriter.toString();
        this.setResult(objectMap, fileName, templateStr);
    }

    @Override
    public String writer2String(Map<String, Object> objectMap, String templatePath, File file) throws IOException {
        return null;
    }
}
